package m202406.d08;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/8 21:26
 */
public class Q2 {


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode();
        ListNode p = res;
        boolean flag = false;
        while (l1 != null || l2 != null) {
            int val = flag ? 1 : 0;
            if (l1 != null) {
                val = val + l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                val = val + l2.val;
                l2 = l2.next;
            }

            flag = val >= 10;
            p.next = new ListNode(val % 10);
            p = p.next;
        }
        if (flag) {
            p.next = new ListNode(1);
        }
        return res.next;
    }
}
